const { defineConfig } = require('@vue/cli-service')
const BodyParser = require('body-parser')

const UserInfo = [
  {
    username: 'admin',
    password: '123',
  },
  {
    username: 'super-admin',
    password: '321',
  },
]

module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        pathRewrite: {
          '^/api': '',
        },
      },
    },
    setupMiddlewares(middlewares, devServer) {
      devServer.app.use(BodyParser.json())

      // 1. 登陆接口
      devServer.app.post('/api/login', (req, res) => {
        const { username, password } = req.body
        // console.log('username:', username)
        // console.log('password:', password)
        const loginStatus = UserInfo.some((v) => v.username === username && v.password === password)
        // console.log('loginStatus:', loginStatus)
        if (loginStatus) {
          res.json({
            code: 1,
            msg: 'suceess',
            data: {
              token: `${username}_123`,
            },
          })
        } else {
          res.json({
            code: -1,
            msg: '登陆失败，请重新尝试',
          })
        }
      })

      return middlewares
    },
  },
})
